package com.ruix.app.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.ruix.app.R;
import com.ruix.app.adapter.superadapter.SuperAdapter;
import com.ruix.app.adapter.superadapter.internal.SuperViewHolder;
import com.ruix.app.manager.UserManager;
import com.ruix.app.model.Notice;
import com.ruix.app.model.Role;
import com.ruix.app.util.RoleUtil;

/**
 * Created by hadexs on 2017/4/21.
 */

public class NoticeListAdapter extends SuperAdapter<Notice> {


    private  NoticeListListener listener;
    private String uid;
    private int greeTextColor;
    private int grayTextColor;
    public NoticeListAdapter(Context context) {
        super(context, R.layout.item_notice_cell);
        uid = UserManager.getCurrentUser().getID();
        greeTextColor = context.getResources().getColor(R.color.green);
        grayTextColor = context.getResources().getColor(R.color.gray);
    }

//    @Override
//    public boolean hasStableIds() {
//        return false;
//    }

    @Override
    public void onBind(SuperViewHolder holder, int viewType, int position, final Notice item) {
        if (item != null) {
            holder.setText(R.id.tv_message_title, item.getTitle());
            holder.setText(R.id.tv_message_content, item.getContent());
            holder.setText(R.id.tv_meessage_time,item.getDisplayCreatedTime());
            if (item.isNeedConfirm()){
                if (uid.equals(item.getAuthorid())) {
                    String cofirmCount = String.format("%d/%d确认", item.getConfirmNum(), item.getMemberNum());
                    holder.setText(R.id.tv_confirm_tag, cofirmCount);
                    if (item.getConfirmNum() == item.getMemberNum()) { //所有人已经确认
                        holder.setTextColor(R.id.tv_confirm_tag, grayTextColor);
                        holder.setEnabled(R.id.tv_confirm_tag,false);
                    } else {
                        holder.setTextColor(R.id.tv_confirm_tag, greeTextColor);
                        holder.setEnabled(R.id.tv_confirm_tag,true);
                        holder.setOnClickListener(R.id.tv_confirm_tag, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (listener != null){
                                    listener.uncomfirmInfo(item);
                                }
                            }
                        });
                    }
                    holder.setVisibility(R.id.tv_confirm_tag, View.VISIBLE);
                    holder.setVisibility(R.id.tv_need_replay_tag, View.GONE);
                } else{
                    holder.setVisibility(R.id.tv_confirm_tag, View.GONE);
                    if (!item.isConfirmed()) {
                        holder.setVisibility(R.id.tv_need_replay_tag, View.VISIBLE);
                    } else {
                        holder.setVisibility(R.id.tv_need_replay_tag, View.GONE);
                    }
                }
            } else {
                holder.setVisibility(R.id.tv_confirm_tag, View.GONE);
                holder.setVisibility(R.id.tv_need_replay_tag, View.GONE);
            }

            if (item.isReaded()){
                holder.setBackgroundResource(R.id.rl_message_contain,R.color.white);
            } else {
                holder.setBackgroundResource(R.id.rl_message_contain,R.color.color_E4F8F3);
            }


            if (item.isShowRoleFrom()) {
                String roleName = RoleUtil.roleDisplayValue(item.getRoleID());
                if (!TextUtils.isEmpty(roleName)) {
                    roleName = String.format("来自于 %s", roleName);
                    holder.setText(R.id.tv_role_meessage, roleName);
                }
                holder.setVisibility(R.id.tv_role_meessage, View.VISIBLE);
            } else {
                holder.setVisibility(R.id.tv_role_meessage, View.GONE);
            }



        }
    }

    public void setListener(NoticeListListener listener) {
        this.listener = listener;
    }

    public interface NoticeListListener{
        void uncomfirmInfo(Notice item);
    }



}
